package com.struc.td;
/*
 * this is the projectiles class that our towerslow uses
 * it has an update method that will direct it towards the
 * targeted enemy
 */

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;

public class ProjectileSlow extends Projectile{
	private float slow;
	private float xPos;
	private float yPos;
	private Texture spritemap;
	private Sprite projectile;
	private Enemy myTarget;
	private int timer;
	boolean dead;

	public ProjectileSlow(Enemy e, float x, float y, float slow){
		this.slow= slow;
		xPos = x;
		yPos = y;
		myTarget = e;
		spritemap = StaticTextures.proj_slow_proj;
		projectile = new Sprite(spritemap,0,0,16,16);
		projectile.setPosition(x,y);
	}
	/* Finds the path to the target and 
	 * moves towards it.
	 */
	public void update() {
		timer += Gdx.graphics.getDeltaTime()*1000;
		if(timer >20){   
			timer -= 20;
			float xTarget = myTarget.getBounds().getX() + myTarget.getBounds().getWidth()/2;
			float yTarget = myTarget.getBounds().getY() + myTarget.getBounds().getHeight()/2;
			Vector2 vec = new Vector2();
			vec.x = xTarget-xPos;
			vec.y = yTarget-yPos;
			float totDistance = (float) Math.sqrt(Math.pow(Math.abs(vec.x) + Math.abs(vec.y),2));
			vec.x = vec.x/totDistance;
			vec.y = vec.y/totDistance;  
			xPos += vec.x * 20;
			yPos += vec.y * 20;
			projectile.setPosition(xPos,yPos);
			if (totDistance < 10) {
				dead = true;
				myTarget.gotHit(this);
			}
		}
	}
	public void render(SpriteBatch sb) {
		projectile.draw(sb);
	}
	public boolean isDead() {
		return dead;
	}

	public float getSlow(){
		return slow;
	}
	// Test methods
	public void setTimer(int t) {
		timer = t;
	}
}